using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using TemplateWeb.DAO;
using TemplateWeb.Helpers;

namespace TemplateWeb.DTO
{
	/// <summary>
	/// Data transfer object which is associated with SanPham table in database
	/// </summary>
	public class SanPhamDTO
	{
		public int ID { get; set; }
		public int IDDanhMucCon { get; set; }
		public int IDToChucCaNhan { get; set; }
		public string TenSanPham { get; set; }
		public string XuatXu { get; set; }
		public string MoTa { get; set; }
		public string HinhAnh { get; set; }
		public int SoLuongTon { get; set; }
        
        public DanhMucConDTO DanhMucCon { get; set; }

		public static SanPhamDTO Translate(DataRow row)
		{
			var dto = new SanPhamDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				IDDanhMucCon = int.Parse(row["IDDanhMucCon"].ToString()),
				IDToChucCaNhan = int.Parse(row["IDToChucCaNhan"].ToString()),
				TenSanPham = row["TenSanPham"].ToString(),
				XuatXu = row["XuatXu"].ToString(),
				MoTa = row["MoTa"].ToString(),
				HinhAnh = row["HinhAnh"].ToString(),
				SoLuongTon = int.Parse(row["SoLuongTon"].ToString()),
			};

            var rpt = new DanhMucConDAO(WebConfiguration.DbConnectionString);
            var objDS = rpt.SelectByID(dto.IDDanhMucCon);

            if (objDS != null && objDS.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow datarow in objDS.Tables[0].Rows)
                {
                    // Add tat ca nguoi choi co tham gia phien dau gia vao list
                    dto.DanhMucCon = new DanhMucConDTO
                    {
                        ID = dto.IDDanhMucCon,
                        IDDanhMucChinh = int.Parse(datarow["IDDanhMucChinh"].ToString()),
                        TenDanhMucCon = datarow["TenDanhMucCon"].ToString(),
                        DanhMucChinh = new DanhMucChinhDTO
                        {
                            ID = int.Parse(datarow["IDDanhMucChinh"].ToString()),
                            TenDanhMucChinh = datarow["TenDanhMucChinh"].ToString()
                        }
                    };
                }
            }

			return dto;
		}
	}
}